package org.hairball.analysis;

import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ConstructorInjectionAnalyser implements RelationshipAnalyser {
    private static final ASMClassDescriptorFilter filter = new ASMClassDescriptorFilter();

    public Set<Relationship> analyse(ClassNode node) {
        Set<Relationship> injectedClasses = new HashSet<Relationship>();
        String fromClassNamde = filter.filter(node.name);

        for (Object method : node.methods) {
            MethodNode methodNode = (MethodNode) method;

            if (methodNode.name.equals("<init>")) {
                DescriptorReader descriptorReader = new DescriptorReader();
                List<String> classNameList = descriptorReader.getParameterTypes(methodNode.desc);

                for (String aClass : classNameList) {
                    String toClassname = filter.filter(aClass);
                    injectedClasses.add(new Relationship(fromClassNamde, toClassname, "CI"));
                }
            }
        }

        return injectedClasses;
    }
}
